import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_core_widget/utils/ex/kq_ex.dart';
import 'package:kq_flutter_core_widget/utils/toast_util.dart';
import 'package:kq_flutter_widgets/widgets/formItem/entity/kq_form_entity.dart';
import 'package:kq_flutter_widgets/widgets/formItem/kq_header_item_view.dart';
import 'package:kq_flutter_widgets/widgets/gridView/kq_selection_order_grid_view.dart';
import 'package:kq_flutter_widgets/widgets/titleBar/kq_title_bar.dart';
import 'package:kq_flutter_widgets/widgets/gridView/kq_selectable_grid_view.dart';

class KqSelectableGridViewDemo extends StatefulWidget {
  const KqSelectableGridViewDemo({super.key});

  @override
  State<StatefulWidget> createState() => KqSelectableGridViewDemoState();
}

class KqSelectableGridViewDemoState extends State<KqSelectableGridViewDemo> {
  List<IGridViewItemData> test = [
    IGridViewItemData("title1", isSelected: false, isUsed: false),
    IGridViewItemData("title2", isSelected: false, isUsed: false),
    IGridViewItemData(
        "很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长",
        isSelected: false,
        isUsed: false),
    IGridViewItemData("title5", isSelected: false, isUsed: true),
    IGridViewItemData("title6", isSelected: false, isUsed: false),
  ];

  List<ISelectionOrderGridViewItemData> test2 = [
    ISelectionOrderGridViewItemData("title1", isSelected: true, index: 1),
    ISelectionOrderGridViewItemData("title2", isSelected: false),
    ISelectionOrderGridViewItemData("title3", isSelected: false),
    ISelectionOrderGridViewItemData("很长很长很长很长很长很长很长很长很长很长很长很长很长很长",
        isSelected: true, index: 4),
    ISelectionOrderGridViewItemData("title5", isSelected: true, index: 5),
    ISelectionOrderGridViewItemData("title6", isSelected: true, index: 2),
    ISelectionOrderGridViewItemData("title7", isSelected: false, group: 1),
    ISelectionOrderGridViewItemData("title8", isSelected: false, group: 1),
    ISelectionOrderGridViewItemData("title9", isSelected: false, group: 1),
    ISelectionOrderGridViewItemData("title0", isSelected: true, index: 3),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KqHeadBar(
        headTitle: '可选择的gridview',
        back: () {
          Get.back();
        },
      ),
      body: Column(
        children: [
          KqHeaderItemView(
            entity: KqFormEntity(
              title: "可选择的KqSelectableGridView演示：",
            ),
          ),
          KqSelectableGridView(
            items: test,
            onTap: (data) {
              if (data != null) {
                KqToast.showNormal(data.title);
              }
            },
          ),
          KqHeaderItemView(
            entity: KqFormEntity(
              title: "可选择排序且有序号的KqSelectionOrderGridView演示：",
            ),
          ),
          KqSelectionOrderGridView(
            items: test2,
            onTap: (data) {
              if (data.isNotNullOrEmpty) {
                KqToast.showNormal(concatDynamic(data, (t) => t.title));
              }
            },
          ),
          KqHeaderItemView(
            entity: KqFormEntity(
              title: "一体机KqCheckSelectionGridView演示：",
            ),
          ),
        ],
      ),
    );
  }

  static String? concatDynamic<T>(
      List<T>? list, String? Function(T t) concatConverter,
      {String split = ',',
      String? defaultValue,
      bool concatWhenEmpty = false}) {
    if (list == null) {
      return defaultValue;
    }
    String str = '';
    for (var value in list) {
      if (str.isEmpty) {
        str = concatConverter(value) ?? '';
      } else {
        str = concatString(str, concatConverter(value), split,
            concatWhenEmpty: concatWhenEmpty);
      }
    }
    return str.isEmpty ? defaultValue : str;
  }

  static String concatString(String? str1, String? str2, String split,
      {bool concatWhenEmpty = false}) {
    if (str1.isNullOrEmpty) {
      if (concatWhenEmpty) {
        return '$split${str2 ?? ''}';
      } else {
        return str2 ?? '';
      }
    }
    if (str2.isNullOrEmpty) {
      if (concatWhenEmpty) {
        return '$str1$split';
      } else {
        return str1!;
      }
    }
    return '$str1$split$str2';
  }
}
